home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: fedinv@ix.netcom.com(Steve Mahoney )
- Newsgroups: comp.lang.c++
- Subject: OOD problem
- Date: 15 Feb 1996 15:49:49 GMT
- Organization: Netcom
- Message-ID: <4fvkmt$ems@reader2.ix.netcom.com>
- NNTP-Posting-Host: ix-pit1-26.ix.netcom.com
- X-NETCOM-Date: Thu Feb 15 7:49:49 AM PST 1996
-
-
- I have two classes which form a handle/body pair. So, one class ( class
- A, the handle) is seen by clients, and class A HAS-A class B ( the body
- ) which is hidden from clients. Hence we have the following situation -
-
- // ...Handle
- class A
- {
- A( int n ) { if ( n == x ) b = new ( C ) ;
- elseif ( n == y ) b = new ( D ) ;
- else b = new (E) ; } ;
- ~A() { } ;
- .
- .
- A_f1 ( ) { b->B_f1( ) ; } ; // forward request to B
- A_f2 ( ) { b->B_f2( ) ; } ; // ô ô
- .
- .
- private:
- B *b ; // A HAS-A B
- } ;
-
- //... Body ( is an ABC )
- class B
- {
- .
- .
- virtual B_f1 ( ) = 0 ;
- virtual B_f2 ( ) ;
- .
- .
- } ;
-
- Class AÆs purpose is to shield clients from the details of class B and
- all it does is merely forward requests to B. Class B is an abstract
- base class for class C, D and E. Classes C,D and E implement the
- functions B_f1, B_f2 etc.. However, class E has its own member
- functions. Hence class E would look something like this -
-
- class E : public B
- {
- .
- .
- B_f1 ( ) { } ;
- B_f2 ( ) { } ;
- E_f1 ( ) { } ;
- .
- .
- } ;
-
- I would like to invoke E_f1() using the b pointer in class A. Hence
- inside A, I would like to do the following -
- class A
- {
- .
- .
- A_f3 ( ) { b->E_f1 () ; } ;
- } ;
-
- This WILL NOT work since we have a B* and E_f1() is not a member
- function in class B.
- We could force this by using a cast as follows -
- class A
- {
- .
- .
- A_f3 ( ) { E* e = (E*)b ; e->E_f1 ( ) ; } ;
- } ;
- but I have make sure that b points to a valid E object inside A_f3 ().
-
- Is there any elegant way to achieve the above without having to cast or
- create a new Handle/Body pair like the following -
- class A2
- {
- .
- .
- private:
- E* e ;
- };
-
- If anyone has any advice to offer, I would be MOST appreciative.
-
- Thanks,
- -- TJ
-
- ***********************************************************************
- *
-
-
-
-
-